package com.spirng.leetcode.day08;

import java.util.*;
import java.util.stream.Collectors;

public class Test491 {
    public static void main(String[] args) {
        Test491 test491 = new Test491();
        test491.findSubsequences(new int[]{4,6,7,7});
    }
    ArrayList lists=new ArrayList<List<Integer>>();
    ArrayList<Integer> list=new ArrayList<Integer>();
    public List<List<Integer>> findSubsequences(int[] nums) {
           backtracking(nums,0);
//        List collect = (List) lists.stream().distinct().collect(Collectors.toList());
        return lists;
    }
    public void backtracking(int [] nums,int offSize){
        HashSet<Integer> set = new HashSet<>();
        if(list.size()>=2){
            lists.add(new ArrayList<>(list));
        }
        for (int i = offSize; i < nums.length; i++) {
            if(!list.isEmpty()&&list.get(list.size()-1)>nums[i]||set.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            set.add(nums[i]);
            backtracking(nums,i+1);
            list.remove(list.size()-1);
        }
    }
}
